import store from '@/store'
import { asyncRoutes, constantRoutes } from '@/router'
const state = {
  routes: []
}
const mutations = {
  setRoutes(state, data) {
    // console.log(constantRoutes)
    // 真正的菜单应该由静态路由+有权限的动态路由拼接而成
    // 有权限的动态路由已经从 data 传入
    state.routes = [...constantRoutes, ...data]
  }
}
const actions = {
  filterRouter() {
    const menus = store.state.user.userInfo.roles.menus
    const routes = asyncRoutes.filter(item => menus.includes(item.name))
    // console.log(routes)
    // 本来, 这个逻辑实在导航守卫的, 封装在这里是为了同时在两个逻辑上共享
    // 1. 回复url访问(靠 return 到导航守卫调用 router.addRoutes)
    // 2. 希望拼接出一个菜单列表,替换原来默认的路由配置(调用 mutations)
    store.commit('permission/setRoutes', routes)
    return routes
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
